package com.lib.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

import com.lib.utils.XLog;

/**
 * @author 一叶丶枫凌
 * Created on 2017-05-15 11:46
 */
public class NoTouchAbleViewPager extends ViewPager {

    private boolean mEnableTouch;
    private float mOrigin;

    public NoTouchAbleViewPager(Context context) {

        this(context, null);
    }

    public NoTouchAbleViewPager(Context context, AttributeSet attrs) {

        super(context, attrs);
    }

    public boolean isEnableTouch() {

        return mEnableTouch;
    }

    public void setEnableTouch(boolean enableTouch) {

        mEnableTouch = enableTouch;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        return mEnableTouch && super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mOrigin = event.getX();
                if (mOrigin < XLog.sContext.getResources().getDisplayMetrics().widthPixels - 100) {
                    mEnableTouch = true;
                    // getParent().requestDisallowInterceptTouchEvent(false);
                    onTouchEvent(event);
                    // super.dispatchTouchEvent(event);
                    getChildAt(getCurrentItem()).dispatchTouchEvent(event);
                } else {
                    // getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return mEnableTouch && super.onTouchEvent(event);
    }

    // @Override
    // public void setCurrentItem(int item, boolean smoothScroll) {
    //
    // 	super.setCurrentItem(item, smoothScroll);
    // }
    // @Override
    // public void setCurrentItem(int item) {
    // 	//false 去除滚动效果
    // 	super.setCurrentItem(item, false);
    // }
}
